/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.permission.controller;

/**
 *
 * @author truong
 */
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;
import queenb.web.util.SessionProvider;
import javax.faces.model.SelectItem;
import queenb.net.entitybean.Q150;
import queenb.web.controller.QueenbController;
import queenb.web.permission.store.Q150Store;

public class Q275Controller extends QueenbController {

    private static Logger logger = Logger.getLogger(Q275Controller.class);
    private List<SelectItem> listQ350;
    private int idQ350 = 1;
    private String listStringLeft;
    private String listStringRight;
    private int acturnReturnCode;
    private String listQ150left;
    private String listQ150right;
    private List<Q150> listQ150R;
    private List<Q150> listQ150L;
    private String detail;
    private List<Q150> listQ150N;
    private List<Q150> listFindQ175;
    private int fq300;

    /**
     * Creates a new instance of AdminQ275ControllerDetail
     */
    public Q275Controller() {
        Object pnPQ350 = getRequest().getParameter("pq350");
        if (pnPQ350 != null) {
            idQ350 = Integer.parseInt(pnPQ350.toString());
            SessionProvider.setSessionMapValue("PQ350", idQ350);
        }
        Object objFq300 = getRequest().getParameter("fq300");
        if (objFq300 != null) {
            this.fq300 = Integer.parseInt(objFq300.toString());
        }
    }

    /**
     * @param listQ350 the listQ350 to set
     */
    public void setListQ350(List<SelectItem> listQ350) {
        this.listQ350 = listQ350;

    }

    /**
     * @return the idQ350
     */
    public int getIdQ350() {
        if (SessionProvider.getSessionMapValue("PQ350") != null) {
            idQ350 = Integer.parseInt(SessionProvider.getSessionMapValue("PQ350").toString());
        }
        return idQ350;
    }

    /**
     * @param idQ350 the idQ350 to set
     */
    public void setIdQ350(int idQ350) {
        this.idQ350 = idQ350;
    }

    /**
     * @param listStringLeft the listStringLeft to set
     */
    public void setListStringLeft(String listStringLeft) {
        this.listStringLeft = listStringLeft;
    }

    /**
     * @param listStringRight the listStringRight to set
     */
    public void setListStringRight(String listStringRight) {
        this.listStringRight = listStringRight;
    }

    public List<Q150> getListQ150N() {
        if (this.idQ350 > 0) {
            try {
                Q150Store q150Store = new Q150Store();
                System.out.println("-----list q150r----" + getLogin().getModule() + "," + this.idQ350 + "," + getLogin().getUserName());
                listQ150N = q150Store.findQ150N(getLogin().getModule(), this.idQ350, getLogin().getUserName());


            } catch (Exception ex) {
                System.out.println("Error when get list Q450L. ERROR CODE: " + ex.getMessage());
            }
        }
        return listQ150N;
    }

    public List<Q150> getListFindQ175() throws SQLException, InstantiationException {
        if (listFindQ175 == null) {
            try {
                Q150Store store = new Q150Store();
                System.out.println("-------listfind----------" + 7 + "," + getFq300() + "," + idQ350 + "," + getLogin().getUserName());
                listFindQ175 = store.findQ150D(getLogin().getModule(), getFq300(), idQ350, getLogin().getUserName());

            } catch (IllegalAccessException ex) {
                //java.util.logging.Logger.getLogger(Q175Controller.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }
        return listFindQ175;

    }

    /**
     * @return the acturnReturnCode
     */
    public int getActurnReturnCode() {

        ArrayList<Integer> front = new ArrayList<Integer>();
        ArrayList<Integer> back = new ArrayList<Integer>();
        if (SessionProvider.getSessionMapValue("PQ350") != null) {
            idQ350 = Integer.parseInt(SessionProvider.getSessionMapValue("PQ350").toString());
        }

        Object isupdate = getRequest().getParameter("values2");

        if (isupdate != null) {
            Q150Store store = new Q150Store();

            String[] array = isupdate.toString().split(",");
            logger.debug("+++++" + isupdate + "++" + array.length + "--" + this.idQ350);
            for (int i = 0; i < array.length; i++) {
                front.add(Integer.parseInt(array[i]));
            }
            //get right list
            if (SessionProvider.getSessionMapValue("PQ350") != null) {
                idQ350 = Integer.parseInt(SessionProvider.getSessionMapValue("PQ350").toString());
            }

            List<Q150> list = new ArrayList<Q150>();
            try {

                list = store.findQ150R(getLogin().getModule(), idQ350, getLogin().getUserName());
                System.out.println("KIch thuot list ben phai co id= " + idQ350 + " la: " + list.size());

            } catch (Exception ex) {
                System.out.println("-------------. ERROR CODE: " + ex.getMessage());
                //showGlobalReport(true, getResourceString("updatefail"));
                return 0;
            }
            for (int i = 0; i < list.size(); i++) {
                back.add(list.get(i).getFq400());
            }
            logger.debug("size" + front.size() + "size" + back.size());

            for (int i = 0; i < front.size(); i++) {
                if (front.get(i) == 0) {
                    continue;
                }

                int result = 0;
                if (!back.contains(front.get(i)) || back.size() == 0) {
                    try {


                        logger.debug("updateeeeeeeeeee" + this.idQ350 + "--" + front.get(i));
                        store.updateQ150(getLogin().getModule(), this.idQ350, front.get(i), getLogin().getUserName());
                    } catch (Exception ex) {
                        //showGlobalReport(true, getResourceString("updatefail"));
                        logger.debug("error " + ex.getMessage() + result);
                        return 0;
                    }
                }
            }
            for (int i = 0; i < back.size(); i++) {
                logger.debug("delete " + back.get(i) + front.contains(back.get(i)));
                if (!front.contains(back.get(i))) {
                    try {

                        logger.debug("delete " + back.get(i) + "with" + this.idQ350);
                        System.out.println("-------- DEL Q150-------"+getLogin().getModule()+","+ this.idQ350+","+ back.get(i)+","+getLogin().getUserName());
                        store.deleteQ150(getLogin().getModule(), this.idQ350, back.get(i), getLogin().getUserName());

                    } catch (Exception ex) {
                        //showGlobalReport(true, getResourceString("updatefail"));
                        logger.debug("error " + ex.getMessage());
                        return 0;
                    }
                }
            }

        }
        // showGlobalReport(true, getResourceString("updatesuccess"));

        return acturnReturnCode;
    }

    /**
     * @param acturnReturnCode the acturnReturnCode to set
     */
    public void setActurnReturnCode(int acturnReturnCode) {
        this.acturnReturnCode = acturnReturnCode;
    }

    /**
     * @return the listQ150left
     */
    public String getListQ150left() {

        List<Q150> list = null;
        String txtLeftString = "";
        String left = "";
        try {
            list = getListQ150L();
        } catch (Exception ex) {
            logger.debug("Error when gQ450. ERROR CODE: " + ex.getMessage());
        }

        if (list != null) {
            int n = 0;
            while (list.size() > 0) {

                int i = 0;
                int flat = 0;
                while (i < list.size()) {
                    if (flat == 0) {
                        n = list.get(0).getFq300();
                        logger.debug("fq300 neeeeeeeeeeeeeeeeeee " + list.get(0).getFq300());
                        left += "<div title='" + n + "' ondblclick='moveRight(this)' onclick='expand(this)' class='group lgroupparent" + list.get(0).getFq300() + "'>" + getResourceString(list.get(0).getQv301()) + "</div>";
                        left += "<div style='display:none' class='child lgroupchild" + list.get(0).getFq300() + "'>";

                        left += "<div class='sourceItem' ondblclick='moveRight(this)' onclick='changeStyleLeft(this)' lang='"
                                + list.get(0).getPq400() + "' title='" + n + "'>" + getResourceString(list.get(0).getQv401()) + "</div>";
                        flat = 1;
                        list.remove(0);
                    } else {

                        Q150 q150 = list.get(i);
                        if (q150.getFq300() == n) {

                            left += "<div class='sourceItem' ondblclick='moveRight(this)' onclick='changeStyleLeft(this)' lang='"
                                    + q150.getPq400() + "' title='" + n + "' >" + getResourceString(q150.getQv401()) + "</div>";
                            list.remove(i);

                        } else {
                            i++;
                        }
                    }

                }
                left += "</div>";
            }
        }
        //



        return left;
    }

    /**
     * @param listQ150left the listQ150left to set
     */
    public void setListQ150left(String listQ150left) {
        this.listQ150left = listQ150left;
    }

    /**
     * @return the listQ150right
     */
    public String getListQ150right() {
        List<Q150> list = null;
        String right = "";
        try {
            list = getListQ150R();
        } catch (Exception ex) {
            logger.debug("Error when gQ450. ERROR CODE: " + ex.getMessage());
        }
//
        if (list != null) {
            int n = 0;
            while (list.size() > 0) {

                int i = 0;
                int flat = 0;
                while (i < list.size()) {
                    if (flat == 0) {
                        n = list.get(0).getFq300();
                        logger.debug("fq300 neeeeeeeeeeeeeeeeeee " + list.get(0).getFq300());
                        right += "<div title='" + n + "' ondblclick='moveLeft(this)' onclick='expand(this)' class='group rgroupparent" + list.get(0).getFq300() + "'>" + getResourceString(list.get(0).getQv301()) + "</div>";
                        right += "<div style='display:none' class='child rgroupchild" + list.get(0).getFq300() + "'>";



                        right += "<div class='destinationItem ' ondblclick='moveLeft(this)' onclick='changeStyleRight(this)' lang='" + list.get(0).getFq400() + "' title='" + n + "'>";
                        right += getResourceString(list.get(0).getQv401());
                        right += "</div>";

                        flat = 1;
                        list.remove(0);
                    } else {

                        Q150 q150 = list.get(i);
                        if (q150.getFq300() == n) {

                            right += "<div class='destinationItem' ondblclick='moveLeft(this)' onclick='changeStyleRight(this)' lang='" + q150.getFq400() + "' title='" + n + "'>";
                            right += getResourceString(q150.getQv401());
                            right += "</div>";
                            list.remove(i);

                        } else {
                            i++;
                        }
                    }

                }
                right += "</div>";
            }


        }
//
        return right;
    }

    /**
     * @param listQ150right the listQ150right to set
     */
    public void setListQ150right(String listQ150right) {
        this.listQ150right = listQ150right;
    }

    /**
     * @return the listQ150R
     */
    public List<Q150> getListQ150R() {

        if (this.idQ350 > 0) {
            try {
                Q150Store q150Store = new Q150Store();
                listQ150R = q150Store.findQ150R(getLogin().getModule(), this.idQ350, getLogin().getUserName());
                System.out.println("KIch thuot list ben phai co id= " + this.idQ350 + " la: " + listQ150R.size());

            } catch (Exception ex) {
                System.out.println("Error when get list Q450L. ERROR CODE: " + ex.getMessage());
            }
        }
        return listQ150R;
    }

    /**
     * @param listQ150R the listQ150R to set
     */
    public void setListQ150R(List<Q150> listQ150R) {
        this.listQ150R = listQ150R;
    }

    /**
     * @return the listQ150L
     */
    public List<Q150> getListQ150L() {

        if (this.idQ350 > 0) {
            try {
                Q150Store q150Store = new Q150Store();
                listQ150L = q150Store.findQ150L(getLogin().getModule(), this.idQ350, getLogin().getUserName());
                System.out.println("KIch thuot list ben trai co id= " + this.idQ350 + "group: " + 1 + " la: " + listQ150L.size());

            } catch (Exception ex) {
                System.out.println("Error when get list Q450L. ERROR CODE: " + ex.getMessage());
            }
        }

        return listQ150L;
    }

    /**
     * @param listQ150L the listQ150L to set
     */
    public void setListQ150L(List<Q150> listQ150L) {
        this.listQ150L = listQ150L;
    }

    /**
     * @return the detail
     */
    public String getDetail() {


        List<Q150> list = null;
        String right = "";
        Object pq350id = getRequest().getParameter("pq350id");
        if (pq350id == null) {
            pq350id = "0";
        }

        try {
            Q150Store q150Store = new Q150Store();
            list = q150Store.findQ150R(getLogin().getModule(), Integer.parseInt(pq350id.toString()), getLogin().getUserName());

        } catch (Exception ex) {
            logger.debug("Error when gQ450. ERROR CODE: " + ex.getMessage());
        }
//
        if (list != null) {
            int n = 0;
            while (list.size() > 0) {

                int i = 0;
                int flat = 0;
                int child = 1;
                while (i < list.size()) {
                    if (flat == 0) {
                        n = list.get(0).getFq300();
                        logger.debug("fq300 neeeeeeeeeeeeeeeeeee " + list.get(0).getFq300());
                        child = 1;

                        right += "<div  style='cursor:pointer;font-weight:bold;' onclick='expand(this)' class='groupparent rowItem'>  +" + " &nbsp; &nbsp; " + getResourceString(list.get(0).getQv301()) + "</div>";
                        right += "<div style='display:none; margin-left:25px' class='groupchild'>";
                        right += "<div  class='rowItem'>";
                        right += child + " - " + getResourceString(list.get(0).getQv401());
                        right += "</div>";

                        flat = 1;
                        list.remove(0);
                        child++;
                    } else {

                        Q150 q150 = list.get(i);
                        if (q150.getFq300() == n) {

                            right += "<div  class='rowItem'>";
                            right += child + " - " + getResourceString(list.get(0).getQv401());
                            right += "</div>";
                            list.remove(i);
                            child++;

                        } else {
                            i++;
                        }
                    }

                }
                right += "</div>";
            }
        }

        return right;
    }

    /**
     * @param detail the detail to set
     */
    public void setDetail(String detail) {
        this.detail = detail;
    }

    /**
     * @return the fq300
     */
    public int getFq300() {
        return fq300;
    }

    /**
     * @param fq300 the fq300 to set
     */
    public void setFq300(int fq300) {
        this.fq300 = fq300;
    }
}
